/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainFrame.java
 *
 * Created on Dec 21, 2011, 6:05:22 PM
 */
package com.haui.filter.gui;

import com.haui.filter.entitys.Anlage;
import com.haui.filter.entitys.Gebaeude;
import com.haui.helper.Object2XML;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Haubrock
 */
public class MainFrame extends javax.swing.JFrame {

   public  ArrayList<Gebaeude> gebaeudeliste;
 
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
        try {
            //Überprüfen ob es bereits eine Serialisierte Liste gibt
            gebaeudeliste=new Object2XML<ArrayList>().deserialize("gebaeude.xml");
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        UpdateDataModelGebaeude();
        selGebaeude.setSelectedIndex(0);
        UpdateDataModelAnlage();
        
    }
    
    public final void UpdateDataModelGebaeude()
    {
        //Gebäude Liste aktualisieren
        if(gebaeudeliste==null)
        {
            gebaeudeliste=new ArrayList<Gebaeude>();
        }
        
        selGebaeude.setModel(new DefaultComboBoxModel(gebaeudeliste.toArray()));
        
        
        
        
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selGebaeude = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        addGebaeude = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabFilter = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        listAnlagen = new javax.swing.JList();
        txtBestand = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txtReserve = new javax.swing.JTextField();
        txtSollbestand = new javax.swing.JTextField();
        addAnlage = new javax.swing.JButton();
        addFilter = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        selGebaeude.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        selGebaeude.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selGebaeudeActionPerformed(evt);
            }
        });

        jLabel1.setText("Gebäude");

        addGebaeude.setText("+");
        addGebaeude.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addGebaeudeActionPerformed(evt);
            }
        });

        tabFilter.setAutoCreateRowSorter(true);
        tabFilter.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabFilter.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
        tabFilter.setDoubleBuffered(true);
        tabFilter.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tabFilter.getTableHeader().setReorderingAllowed(false);
        jScrollPane2.setViewportView(tabFilter);
        tabFilter.getAccessibleContext().setAccessibleName("");
        tabFilter.getAccessibleContext().setAccessibleDescription("");

        listAnlagen.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listAnlagen.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listAnlagenValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listAnlagen);

        txtBestand.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtBestand.setText("0");

        jLabel2.setText("Bestand / Soll:");

        jButton1.setText("Aktualisieren");

        jLabel3.setText("Reserve:");

        txtReserve.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtReserve.setText("0");

        txtSollbestand.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSollbestand.setText("0");

        addAnlage.setText("+");
        addAnlage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addAnlageActionPerformed(evt);
            }
        });

        addFilter.setText("+");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(selGebaeude, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addAnlage)
                            .addComponent(addGebaeude)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2))
                                .addGap(19, 19, 19)
                                .addComponent(txtBestand, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtSollbestand, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 367, Short.MAX_VALUE)
                                        .addComponent(addFilter))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(txtReserve, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButton1))))
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 594, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(selGebaeude, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(addGebaeude))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 327, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(txtSollbestand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtBestand, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(addFilter))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtReserve, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jButton1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 129, Short.MAX_VALUE)
                        .addComponent(addAnlage)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void addGebaeudeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addGebaeudeActionPerformed

    AddGebaeudeFrame addgebframe=new AddGebaeudeFrame(this);
    addgebframe.setVisible(true);
    
    
}//GEN-LAST:event_addGebaeudeActionPerformed

private void listAnlagenValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_listAnlagenValueChanged

    UpdateDataModelFilter();
}//GEN-LAST:event_listAnlagenValueChanged

private void selGebaeudeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selGebaeudeActionPerformed

    UpdateDataModelAnlage();
}//GEN-LAST:event_selGebaeudeActionPerformed

    private void addAnlageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addAnlageActionPerformed
        
        AddAnlageFrame frame=new AddAnlageFrame(this);
        frame.setVisible(true);
    }//GEN-LAST:event_addAnlageActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addAnlage;
    private javax.swing.JButton addFilter;
    private javax.swing.JButton addGebaeude;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JList listAnlagen;
    private javax.swing.JComboBox selGebaeude;
    private javax.swing.JTable tabFilter;
    private javax.swing.JTextField txtBestand;
    private javax.swing.JTextField txtReserve;
    private javax.swing.JTextField txtSollbestand;
    // End of variables declaration//GEN-END:variables

    public final void UpdateDataModelAnlage() {
        

        DefaultListModel<Anlage> model=new DefaultListModel<Anlage>();
        Gebaeude selected=getSelectedGebaeude();

        ArrayList<Anlage> anlagen=selected.getAnlagen();
        if(anlagen==null)
        {
            listAnlagen.setModel(model);
            return;
        }
        for(Anlage a:selected.getAnlagen())
        {
            model.addElement(a);
        }
        listAnlagen.setModel(model);
        
    }

    private void UpdateDataModelFilter() {
        
        Anlage selected=getSelectedAnlage();
        
        DefaultTableModel model=new DefaultTableModel();
        String[] columNames={"Maße","Klasse","Art","Typ","Soll","Ist","Reserve"};
        if(selected!=null && selected.getFilter()!=null)
        {   
            Object[][] rows=new Object[selected.getFilter().size()][];
            for(int i=0;i<selected.getFilter().size();i++)
            {
                rows[i]=selected.getFilter().get(i).toArray();
            }
            model=new DefaultTableModel( rows, columNames );
        }
        tabFilter.setModel(model);
        
        
    }

    public Anlage getSelectedAnlage() {
        return (Anlage)listAnlagen.getSelectedValue();
    }
    
    public Gebaeude getSelectedGebaeude() {
        return (Gebaeude)selGebaeude.getSelectedItem();
    }

    void writeXML() {
        try {
            new Object2XML<ArrayList>().serialize("gebaeude.xml", gebaeudeliste);
        } catch (Exception ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
